<?php
abstract class Dealscombined_Models_ModelAbstract {

    public function __construct(array $options = null)
    {
        if (is_array($options)) {
            $this->setOptions($options);
        }
    }

    public function __set($name, $value)
    {
        //$method = 'set' . ucfirst($name);
        $method = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $name)));
        if (('mapper' === $name) || !method_exists($this, $method)) {
            throw new Zend_Exception('Invalid page property');
        }
        $this->$method($value);
    }

    public function __get($name)
    {
        $method = 'get' . ucfirst($name);
        if (('mapper' === $name) || !method_exists($this, $method)) {
            throw new Zend_Exception('Invalid page property');
        }
        return $this->$method();
    }


    public function setOptions(array $options)
    {
        $methods = get_class_methods($this);
        foreach ($options as $key => $value) {
            $method = 'set' . ucfirst($this->_removeUnderScore($key));
            if (in_array($method, $methods)) {
                $this->$method($value);
            }
        }
        return $this;
    }

    protected function _removeUnderScore($column) {
        $colArray = explode("_",$column);

        $property = NULL;
        $numWords = count($colArray);
        for($n=0;$n<$numWords;$n++) {
            if ($n > 0) {
                $property .= ucwords($colArray[$n]);
            } else {
                $property .= $colArray[$n];
            }
        }
        return $property;
    }

    public function toArray()
    {
        $data = array();
        foreach ($this as $key => $property) {
            $key = str_replace("_", "", $key);
            if ($property instanceof Dealscombined_Models_ModelAbstract) {
                $data[$key] = $property->toArray();
            } else {
                $data[$key] = $property;
            }
        }
        return $data;
    }

}